package com.luchao.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.List;

/**
 * 壁纸采集
 * https://mylivewallpapers.com/
 */
public class HtmlParseUtilWallpaper {
    private static Integer num = 10;

    public static void main(String[] args) throws IOException {
        //类型
        String url = "";
        //页数
        Integer num = 326;
        for (int i = 1; i <= num; i++) {
            url = "https://mylivewallpapers.com/page/" + i + "/";
            try {
                Wallpaper(url);
            } catch (Exception e) {
                continue;
            }
        }
    }

    /**
     * 所有的 https://mylivewallpapers.com/page/1/     1-326
     * 动漫  https://mylivewallpapers.com/category/anime/page/2/    1-91
     * 游戏  https://mylivewallpapers.com/category/games/page/2/    1-80
     * 幻想  https://mylivewallpapers.com/category/fantasy/page/2/  1-37
     * 自然界 https://mylivewallpapers.com/category/nature/page/2/   1-79
     * 科幻小说 https://mylivewallpapers.com/category/sci-fi/page/2/  1-21
     * 抽象 https://mylivewallpapers.com/category/abstract/page/2/   1-10
     * 动物 https://mylivewallpapers.com/category/animals/page/2/   1-6
     * 城市 https://mylivewallpapers.com/category/city/page/2/   1-5
     * 漫威 https://mylivewallpapers.com/category/comics/page/2/  1-6
     * 电视电影 https://mylivewallpapers.com/category/movies/page/2/ 1-12
     * 汽车 https://mylivewallpapers.com/category/cars/page/2/ 1-13
     * 生活 https://mylivewallpapers.com/category/lifestyle/page/2/   1-16
     * 爱 https://mylivewallpapers.com/category/love/page/2/  1-5
     * 恐怖 https://mylivewallpapers.com/category/scary/page/2/ 1-4
     *
     * 壁纸下载
     *
     * @throws IOException
     */
    private static void Wallpaper(String url) throws IOException {
//        String url = "https://mylivewallpapers.com/page/100/";
        Document document = Jsoup.parse(new URL(url), 30000);
        Elements element = document.getElementsByClass("posts group");
        List<Node> childNodes = element.get(0).childNodes();
        for (Node n : childNodes) {
//            System.out.println("+"+n.toString());
            String str = n.toString();
            if (str.contains("a href=")) {
                String urlStr = str.substring(str.indexOf("href=") + 6, str.indexOf("\" id="));
//                System.out.println("url:"+urlStr);
                Document ndocument = Jsoup.parse(new URL(urlStr), 30000);
                Elements source = ndocument.getElementsByTag("source");
//                System.out.println("source:"+source);
//                System.out.println(source.get(0).attributes());
                System.out.println(source.get(0).attributes().get("src"));
            }
        }

    }


}
